package com.obroom.calculator;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 简单的计算器
 * 练习
 * Created by ob on 2017/2/20.
 */
public class Calculator {
    private JPanel calculatorView;

    private JTextField resultsTxt;//计算结果

    private JButton zeroBtn;
    private JButton oneBtn;
    private JButton twoBtn;
    private JButton threeBtn;
    private JButton fourBtn;
    private JButton fiveBtn;
    private JButton sixBtn;
    private JButton sevenBtn;
    private JButton eightBtn;
    private JButton nineBtn;
    private JButton multiplyBtn;
    private JButton minusBtn;

    private JButton clearBtn;
    private JButton divideBtn;
    private JButton signBtn;
    private JButton percentBtn;
    private JButton addBtn;
    private JButton digitBtn;
    private JButton equalBtn;

    private Double leftOperand;//左边操作数
    private Double rightOperand;//右边操作数
    private Operation calcOperation;//操作符

    public Calculator() {
        zeroBtn.addActionListener(new NumberBtnClicked(zeroBtn.getText()));
        oneBtn.addActionListener(new NumberBtnClicked(oneBtn.getText()));
        twoBtn.addActionListener(new NumberBtnClicked(twoBtn.getText()));
        threeBtn.addActionListener(new NumberBtnClicked(threeBtn.getText()));
        fourBtn.addActionListener(new NumberBtnClicked(fourBtn.getText()));
        fiveBtn.addActionListener(new NumberBtnClicked(fiveBtn.getText()));
        sixBtn.addActionListener(new NumberBtnClicked(sixBtn.getText()));
        sevenBtn.addActionListener(new NumberBtnClicked(sevenBtn.getText()));
        eightBtn.addActionListener(new NumberBtnClicked(eightBtn.getText()));
        nineBtn.addActionListener(new NumberBtnClicked(nineBtn.getText()));

        addBtn.addActionListener(new OperationBtnClicked(Operation.ADDITION));
        minusBtn.addActionListener(new OperationBtnClicked(Operation.SUBTRACTION));
        multiplyBtn.addActionListener(new OperationBtnClicked(Operation.MULTIPLICATION));
        divideBtn.addActionListener(new OperationBtnClicked(Operation.DIVISION));
        percentBtn.addActionListener(new OperationBtnClicked(Operation.PERCENTAGE));

        clearBtn.addActionListener(new ClearBtnClicked());
        signBtn.addActionListener(new SignBtnClicked());
        digitBtn.addActionListener(new DigitBtnClicked());
        equalBtn.addActionListener(new EqualBtnClicked());
    }


    private class NumberBtnClicked implements ActionListener {
        private String value;
        public NumberBtnClicked(String value) {
            this.value = value;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (leftOperand == null || leftOperand == 0.0) {
                value = resultsTxt.getText() + e.getActionCommand();
            } else {
                if (rightOperand == null || rightOperand == 0.0) {
                    value = e.getActionCommand();
                } else {
                    value = resultsTxt.getText() + e.getActionCommand();
                }
                rightOperand = Double.valueOf(value);
            }
            resultsTxt.setText(value);
        }
    }


    private class OperationBtnClicked implements ActionListener {
        private Operation operation;
        public OperationBtnClicked(Operation operation) {
            this.operation = operation;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            calcOperation = operation;
            if (rightOperand == null || rightOperand == 0.0) {
                leftOperand = Double.valueOf(resultsTxt.getText());
            } else {
                Double output = calcOperation.getOperator().applyAsDouble(leftOperand, rightOperand);
                String result = output % 1 == 0 ? String.valueOf(output.intValue()) : String.valueOf(output);
                resultsTxt.setText(result);
                leftOperand = Double.valueOf(resultsTxt.getText());
            }
            rightOperand = 0.0;
        }
    }

    private class ClearBtnClicked implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            resultsTxt.setText("");
            leftOperand = 0.0;
            rightOperand = 0.0;
        }
    }

    private class DigitBtnClicked implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(!resultsTxt.getText().contains(".")) {
                resultsTxt.setText(resultsTxt.getText() + ".");
            }
        }
    }

    private class EqualBtnClicked implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            Double output = calcOperation.getOperator().applyAsDouble(leftOperand, rightOperand);
            String result = output % 1 == 0 ? String.valueOf(output.intValue()) : String.valueOf(output);
            resultsTxt.setText(result);
            leftOperand = Double.valueOf(resultsTxt.getText());
            rightOperand = 0.0;
        }

    }

    private class SignBtnClicked implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(!resultsTxt.getText().contains("-")) {
                resultsTxt.setText("-" + resultsTxt.getText());
            } else {
                resultsTxt.setText(resultsTxt.getText().substring(1));
            }
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Calculator");
        frame.setContentPane(new Calculator().calculatorView);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
